package com.fpx.open.platform.common.util;

import java.security.MessageDigest;

import sun.misc.BASE64Encoder;

/**
 * MD5工具类
 * 
 * @author sunkey
 * @date May 10, 2013 1:48:44 PM
 * @version 1.0.0
 */
public class MD5Util {

    private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d",
            "e", "f" };

    /**
     * Computes the MD5 fingerprint of a string.
     * 
     * @return the MD5 digest of the input <code>String</code>
     */
    public static String compute(String strInput) {
        try {
            byte[] byteArray = strInput.getBytes();

            MessageDigest md5 = MessageDigest.getInstance("MD5");

            byte[] md5Bytes = md5.digest(byteArray);

            StringBuffer hexValue = new StringBuffer();

            for (int i = 0; i < md5Bytes.length; i++) {
                int val = ((int) md5Bytes[i]) & 0xff;

                if (val < 16)
                    hexValue.append("0");

                hexValue.append(Integer.toHexString(val));
            }

            return hexValue.toString();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * MD5 摘要加密
     * 
     * @param origin
     * @return
     */
    public static String encode(String origin) {
        return encode(origin, null);
    }

    /**
     * MD5 摘要加密
     * 
     * @param origin
     * @param charsetName 编码格式
     * @return
     */
    public static String encode(String origin, String charsetName) {
        String resultString = null;
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            if (charsetName == null || "".equals(charsetName))
                resultString = byteArrayToHexString(md.digest(origin.getBytes()));
            else
                resultString = byteArrayToHexString(md.digest(origin.getBytes(charsetName)));
        } catch (Exception exception) {
        }
        return resultString;
    }

    private static String byteArrayToHexString(byte b[]) {
        StringBuffer resultSb = new StringBuffer();
        for (int i = 0; i < b.length; i++)
            resultSb.append(byteToHexString(b[i]));

        return resultSb.toString();
    }

    private static String byteToHexString(byte b) {
        int n = b;
        if (n < 0)
            n += 256;
        int d1 = n / 16;
        int d2 = n % 16;
        return hexDigits[d1] + hexDigits[d2];
    }

    @SuppressWarnings("restriction")
    public static String Md5Base64(String str) {
        String encodeStr = "";
        try {
            byte[] utfBytes = str.getBytes("UTF-8");
            MessageDigest mdTemp = MessageDigest.getInstance("MD5");
            mdTemp.update(utfBytes);
            byte[] md5Bytes = mdTemp.digest();
            BASE64Encoder b64Encoder = new sun.misc.BASE64Encoder();
            encodeStr = b64Encoder.encode(md5Bytes);
        } catch (Exception e) {
            return encodeStr;
        }
        return encodeStr;
    }

}